const v = require('../Model/ConstValue');

cc.Class({
    extends: cc.Component,

    properties: {
        defaultFrame: {
            default: null,
            type: cc.SpriteFrame
        }
    },

    // use this for initialization
    onLoad: function () {
        //this.model = null;
        this.isSelect = false;
    },

    initWithModel(model) {
        this.model = model;
        var x = model.startX;
        var y = model.startY;
        let pos = this.calculatePixel(x,y);
        this.node.x = pos.posX;
        this.node.y = pos.posY;
        var animation = this.node.getComponent(cc.Animation);
        if (model.status == v.CELL_STATUS.COMMON) {
            animation.stop();
        }
        else {
            animation.play(model.status);
        }
    },

    // 执行移动动作
    updateView() {
        let cmd = this.model.cmd;
        if (cmd.length < 1) {
            return;
        }
        var actions = [];
        var curTime = 0;
        for (let item of cmd) {
            if (item.playTime > curTime) {
                let delay = cc.delayTime(item.playTime - curTime);
                actions.push(delay);
            }
            if (item.action == "moveTo") {
                let pos = this.calculatePixel(item.pos.x, item.pos.y);
                let move = cc.moveTo(v.ANITIME.TOUCH_MOVE, cc.v2(pos.posX, pos.posY));
                actions.push(move);
            }
            else if (item.action == "toDie") {
                if (this.status == v.CELL_STATUS.BIRD) {
                    let animation = this.node.getComponent(cc.Animation);
                    animation.play("effect");
                    actions.push(cc.delayTime(v.ANITIME.BOMB_BIRD_DELAY));
                }
                let callFunc = cc.callFunc(function () {
                    this.node.destroy();
                }, this);
                actions.push(callFunc);
            }
            else if (item.action == "setVisible") {
                let isVisible = item.isVisible;
                actions.push(cc.callFunc(function () {
                    if (isVisible) {
                        this.node.opacity = 255;
                    }
                    else {
                        this.node.opacity = 0;
                    }
                }, this));
            }
            else if (item.action == "toShake") {
                let rotateRight = cc.rotateBy(0.06, 30);
                let rotateLeft = cc.rotateBy(0.12, -60);
                actions.push(cc.repeat(cc.sequence(rotateRight, rotateLeft, rotateRight), 2));
            }
            curTime = item.playTime + item.keepTime;
        }
        // 执行动作
        if (actions.length == 1) {
            this.node.runAction(actions[0]);
        }
        else {
            this.node.runAction(cc.sequence(...actions));
        }

    },
    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
    setSelect(isSelected) {
        var animation = this.node.getComponent(cc.Animation);
        var bg = this.node.getChildByName("select");
        if (isSelected == false && this.isSelect && this.model.status == v.CELL_STATUS.COMMON) {
            animation.stop();
            this.node.getComponent(cc.Sprite).spriteFrame = this.defaultFrame;
        }
        else if (isSelected && this.model.status == v.CELL_STATUS.COMMON) {
            animation.play(v.CELL_STATUS.CLICK);
        }
        else if (isSelected && this.model.status == v.CELL_STATUS.BIRD) {
            animation.play(v.CELL_STATUS.CLICK);
        }
        bg.active = isSelected;
        this.isSelect = isSelected;
    },

    calculatePixel(x, y) {
        const posX = (v.CELL_WIDTH+ v.OFFSET_X)*(y - 0.5);
        const posY = v.GRID_HEIGHT - (v.OFFSET_Y+ v.CELL_HEIGHT)*(x-0.5);
        return {posX, posY};
    }
});
